package com.xuxin.baby.adapter;

import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.ArrayUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.RegexUtils;
import com.blankj.utilcode.util.StringUtils;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.xuxin.baby.R;
import com.xuxin.baby.bean.ReaderVideoBean;
import com.xuxin.baby.glide.GlideApp;
import com.xuxin.baby.network.Retrofit;

import org.jetbrains.annotations.NotNull;

import java.util.List;

/**
 * Created by xu_xin
 * Created on 2020.08.28 0028
 * 精婴早教推荐
 */
public class TeachVideoAdapter extends BaseQuickAdapter<ReaderVideoBean.ResultBean.ItemsBean, BaseViewHolder> {
    RequestOptions options = new RequestOptions().transform(new CenterCrop(), new RoundedCorners(10));

    public TeachVideoAdapter() {
        super(R.layout.item_teach_video);
    }

    @Override
    protected void convert(@NotNull BaseViewHolder holder, ReaderVideoBean.ResultBean.ItemsBean item) {
        holder.setText(R.id.item_teach_video_name, item.getTitle());
        TextView item_teach_video_tag1 = holder.getView(R.id.item_teach_video_tag1);
        TextView item_teach_video_tag2 = holder.getView(R.id.item_teach_video_tag2);
        item_teach_video_tag1.setVisibility(View.GONE);
        item_teach_video_tag2.setVisibility(View.GONE);
        /**
         * 加载图片
         */
        ImageView item_teach_video_image = holder.getView(R.id.item_teach_video_image);
        String videoImage = Retrofit.image_url + item.getImageUrl();
        LogUtils.e("视频文章_image = " + videoImage);
        GlideApp.with(getContext()).load(videoImage).placeholder(R.mipmap.img_placeholder260).apply(options).into(item_teach_video_image);
        if (!StringUtils.isEmpty(item.getTags())) {
            String[] strings = RegexUtils.getSplits(item.getTags(), ",");
            List<String> tags = ArrayUtils.asArrayList(strings);
            if (tags.size() == 1) {
                item_teach_video_tag1.setText(tags.get(0));
                item_teach_video_tag1.setVisibility(View.VISIBLE);
            } else if (tags.size() > 1) {
                item_teach_video_tag1.setText(tags.get(0));
                item_teach_video_tag2.setText(tags.get(1));
                item_teach_video_tag1.setVisibility(View.VISIBLE);
                item_teach_video_tag2.setVisibility(View.VISIBLE);
            }
        }
        /**
         * 免费付费
         */
        ImageView item_teach_video_free = holder.getView(R.id.item_teach_video_free);
        if (item.isFree()) {
            GlideApp.with(getContext()).load(R.mipmap.img_free).apply(options).into(item_teach_video_free);
        } else {
            GlideApp.with(getContext()).load(R.mipmap.img_free_false).apply(options).into(item_teach_video_free);
        }
    }
}